/**
 * Created by Administrator on 2017/6/13.
 */
var host = "103.79.78.153";
$(function () {
    // 表单初始化
    $("#submitForm").validate({
        rules: {
            firstDbHost: {
                required:true
            },
            firstDbName: {
                required:true
            },
            firstDbUsername: {
                required:true
            },
            firstDbPassword: {
                required:true
            },
            secondDbHost: {
                required:true
            },
            secondDbName: {
                required:true
            },
            secondDbUsername: {
                required:true
            },
            secondDbPassword: {
                required:true
            }
        },
        messages: {
            firstDbHost: {
                required: "请输入第一个数据库ip"
            },
            firstDbName: {
                required: "请输入第一个数据库名称"
            },
            firstDbUsername: {
                required: "请输入第一个数据库登入用户名"
            },
            firstDbPassword: {
                required: "请输入第一个数据库登入用户名密码"
            },
            secondDbHost: {
                required: "请输入第二个数据库ip"
            },
            secondDbName: {
                required: "请输入第二个数据库名称"
            },
            secondDbUsername: {
                required: "请输入第二个数据库登入用户名"
            },
            secondDbPassword: {
                required: "请输入第二个数据库登入用户名密码"
            }

        },
        submitHandler: function() {
            submit();
        }
    });
});

function submit() {
    var $data = $('#submitForm').serializeArray();
    var postData = {};
    $.each($data, function(n,v) {
        //循环数组，把数组的每一项都添加到对象中
        postData[$data[n].name] = $data[n].value;
    });

    $.ajax({
        type: "GET",
        url: "http://"+host+":8090/dbcompare/v1/users",
        data: $.param(postData),
        dataType: "JSON",
        async: false,
        success:function(result){
            var $result = $("#result");
            var resultCompare = "";
            $result.html(resultCompare);
            $.each(result.str, function (i,item) {
                resultCompare += result.str[i];
            });
            $result.append(resultCompare);
        },
        error:function(result){
            var resultToJson = eval("(" + result.responseText + ")");
            alert(resultToJson.message);
        }
    });
}